﻿namespace Huayou.Common
{
    using System;
    using System.IO;
    using System.Net;
    using System.Text;

    public class Browser
    {
        private bool m_allowAutoRedirect = false;
        private WebHeaderCollection m_headers = null;
        private HttpWebRequest m_request = null;
        private CookieContainer m_requestCookies = null;
        private HttpWebResponse m_response = null;
        private CookieCollection m_responseCookies = null;

        public void AddCookie(Cookie cookie)
        {
            this.RequestCookies.Add(cookie);
        }

        public void AddCookie(string name, string cookievalue, string path, string domain)
        {
            this.RequestCookies.Add(new Cookie(name, cookievalue, path, domain));
        }

        public string GetContent(string url)
        {
            HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
            request.AllowAutoRedirect = this.AllowAutoRedirect;
            request.CookieContainer = this.RequestCookies;
            HttpWebResponse response = (HttpWebResponse) request.GetResponse();
            Encoding encoding = Encoding.GetEncoding("gb2312");
            StreamReader reader = new StreamReader(response.GetResponseStream(), encoding);
            string str = reader.ReadToEnd();
            reader.Close();
            response.Close();
            this.Headers = response.Headers;
            this.ResponseCookies = response.Cookies;
            this.m_request = request;
            this.m_response = response;
            return str;
        }

        public bool AllowAutoRedirect
        {
            get
            {
                return this.m_allowAutoRedirect;
            }
            set
            {
                this.m_allowAutoRedirect = value;
            }
        }

        public WebHeaderCollection Headers
        {
            get
            {
                return this.m_headers;
            }
            set
            {
                this.m_headers = value;
            }
        }

        public HttpWebRequest Request
        {
            get
            {
                return this.m_request;
            }
        }

        public CookieContainer RequestCookies
        {
            get
            {
                if (this.m_requestCookies == null)
                {
                    this.m_requestCookies = new CookieContainer();
                }
                return this.m_requestCookies;
            }
            set
            {
                this.m_requestCookies = value;
            }
        }

        public HttpWebResponse Response
        {
            get
            {
                return this.m_response;
            }
        }

        public CookieCollection ResponseCookies
        {
            get
            {
                return this.m_responseCookies;
            }
            set
            {
                this.m_responseCookies = value;
            }
        }
    }
}

